package com.tcfvic.client.panels.reports;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.tcfvic.client.PersonService;
import com.tcfvic.client.PersonServiceAsync;
import com.tcfvic.client.ReportService;
import com.tcfvic.client.ReportServiceAsync;
import com.tcfvic.client.panels.widgets.SimpleMessagePopup;
import com.tcfvic.client.pojos.Workercompany;
import com.tcfvic.client.tools.Tools;

public class NewReports {

	private final ReportServiceAsync reportService = GWT
			.create(ReportService.class);
	private final PersonServiceAsync personService = GWT
			.create(PersonService.class);
	private ListBox year = new ListBox();
	private ListBox companies = new ListBox();
	private ListBox sites = new ListBox();

	public NewReports() {
		personService.getAllCompanies(new AsyncCallback<List<Workercompany>>() {

			@Override
			public void onSuccess(List<Workercompany> result) {
				companies.addItem("", "");
				sites.addItem("", "");
				Set <String> companiesSet = new HashSet<String>();
				Set <String> sitesSet = new HashSet<String>();
				for (Workercompany company : result) {
					companiesSet.add(company.getName());
					sitesSet.add(company.getSite());
				}
				for (String s : companiesSet) {
					companies.addItem(s, s);
				}
				for (String s : sitesSet) {
					sites.addItem(s, s);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

		});
		year = Tools.getYearListBox(2000);

	}

	public Widget getContent() {
		VerticalPanel contents = new VerticalPanel();
		contents.add(getTopFilter());
		contents.add(new HTML("<br><hr>"));
		contents.add(getPersonInfoReport());
		contents.add(new HTML("<br><hr>"));
		contents.add(getCompInfoReport());
		contents.add(new HTML("<br><hr>"));
		contents.add(getCourseInfoReport());
		contents.add(new HTML("<br><hr>"));
		contents.add(getExpenseReport());
		contents.add(new HTML("<br><hr>"));
		contents.add(getEmpStatusReport());
		contents.add(new HTML("<br><hr>"));
		contents.add(getJSAPRegReport());
		contents.add(new HTML("<br><hr>"));
		contents.add(getJSAPContDetailsReport());
		contents.add(new HTML("<br><hr>"));
		contents.add(getCompaniesReport());
		contents.add(new HTML("<br><hr>"));
		return contents;
	}

	private Widget getTopFilter() {
		VerticalPanel contents = new VerticalPanel();

		contents.add(new HTML(
				"Optional: Filter report below by the following criteria"));

		HorizontalPanel hp = new HorizontalPanel();
		hp.add(new HTML("Year:"));
		hp.add(year);
		hp.add(new HTML("&nbsp;&nbsp;Company:"));
		hp.add(companies);
		hp.add(new HTML("&nbsp;&nbsp;Site:"));
		hp.add(sites);
		contents.add(hp);

		return contents;
	}

	private int getIntValue(ListBox list) {
		String strValue = getStringValue(list);
		if (strValue != null && strValue.length() > 0) {
			return Integer.valueOf(getStringValue(list));
		} else {
			return 0;
		}
	}

	private String getStringValue(ListBox list) {
		String value = "";
		if (list.getSelectedIndex() > 0) {
			value = list.getValue(list.getSelectedIndex());
		}
		return value;
	}

	private Widget getCompInfoReport() {
		HTML link = new HTML("Company Information Report");
		link.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent event) {
				reportService.getCompInfoReport(getIntValue(year),
						getStringValue(companies), getStringValue(sites),
						new ASyncReportCallBack());
			}
		});
		return link;
	}

	private Widget getCourseInfoReport() {
		HTML link = new HTML("Course Information Report");
		link.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent event) {
				reportService.getCourseInfoReport(getIntValue(year),
						getStringValue(companies), getStringValue(sites),
						new ASyncReportCallBack());
			}
		});
		return link;
	}
	
	private Widget getCompaniesReport() {
		HTML link = new HTML("Companies Report");
		link.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent event) {
				reportService.getCompaniesReport(getIntValue(year),
						getStringValue(companies), getStringValue(sites),
						new ASyncReportCallBack());
			}
		});
		return link;
	}
	
	private Widget getExpenseReport() {
		HTML link = new HTML("Expense Report");
		link.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent event) {
				reportService.getExpenseReport(getIntValue(year),
						getStringValue(companies), getStringValue(sites),
						new ASyncReportCallBack());
			}
		});
		return link;
	}
	
	private Widget getEmpStatusReport() {
		HTML link = new HTML("Employment Status Report");
		link.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent event) {
				reportService.getEmpStatusReport(getIntValue(year),
						getStringValue(companies), getStringValue(sites),
						new ASyncReportCallBack());
			}
		});
		return link;
	}

	private Widget getPersonInfoReport() {
		HTML link = new HTML("Personal Information Report");
		link.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent event) {
				reportService.getPIInfoReport(getIntValue(year),
						getStringValue(companies), getStringValue(sites),
						new ASyncReportCallBack());
			}
		});
		return link;
	}
	
	private Widget getJSAPRegReport() {
		HTML link = new HTML("Worker JSAP Registration Report");
		link.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent event) {
				reportService.getJSAPRegReport(getIntValue(year),
						getStringValue(companies), getStringValue(sites),
						new ASyncReportCallBack());
			}
		});
		return link;
	}
	
	private Widget getJSAPContDetailsReport() {
		HTML link = new HTML("JSAP Contact Details Report");
		link.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent event) {
				reportService.getJSAPContDetailsReport(getIntValue(year),
						getStringValue(companies), getStringValue(sites),
						new ASyncReportCallBack());
			}
		});
		return link;
	}
	
	private void handleError(Throwable caught) {
		GWT.log("Error occured getting all person data", caught);
		new SimpleMessagePopup("Error occured getting all person data", null)
				.showPopup();
	}

	/**
	 * Private callback method.
	 * 
	 * @author Dave
	 * 
	 */
	private class ASyncReportCallBack implements AsyncCallback<String> {
		@Override
		public void onFailure(Throwable caught) {
			handleError(caught);
		}

		@Override
		public void onSuccess(String result) {
			Window.open(result, "", "");
		}
	}
}
